import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

// 导入获取用户信息接口 & 购物车数据
import { getUserInfo, getCartList } from "@/api";


export default new Vuex.Store({
    //保存用户数据
    state: {
        token: sessionStorage.getItem('token') ? sessionStorage.getItem('token') : '',
        isLogin: sessionStorage.getItem('isLogin') ? sessionStorage.getItem('isLogin') : false,
        userInfo: null,
        carList: [],
        badgecount: 0,
        allChecked: false,
        img: '',
    },
    getters: {
        getIsLogin(state) {
            return state.isLogin
        },
        getToken(state) {
            return state.token
        },
        getBge(state) {
            return state.badgecount
        },
        getCartData(state) {
            return state.carList
        },
        //直接获取总价格，当cartList数据发生变化 
        //总价计算逻辑在使用总价数据的时候会重新执行
        getCountPrie(state) {
            let countPrice = 0
            state.carList.forEach(item => {
                if (item.checked) {
                    countPrice += (item.count * item.price)
                }
            })
            return countPrice
        },
        //用户登录头像
        getImg(state) {
            return state.img
        }
    },
    mutations: {
        setToken(state, params) {
            sessionStorage.setItem('token', params.token)
            sessionStorage.setItem('isLogin', true)
            state.token = params.token
            state.isLogin = true
        },
        // 定义用于更新数据的方法
        setUserInfo(state, params) {
            state.userInfo = params
        },
        setCarList(state, params) {
            state.carList = params
        },
        setBadge(state, params) {
            state.badgecount = params
        },
        setAllchecked(state, params) {
            state.allChecked = params
        },
        setImg(state, params) {
            sessionStorage.setItem('img', params)
            state.img = params
        },
        setIsLogin(state, params) {
            state.isLogin = params
        }


    },
    actions: {
        //检测是否登录
        getInfo(state) {
            getUserInfo().then((res => {
                state.commit('setUserInfo', res.data);
            }))
        },
        getCartData(state) {
            getCartList().then((res) => {
                // 从购物车数据中得到 总商品数据量
                const list = res
                let count = 0
                list.forEach(element => {
                    // 得到每件商品的数据
                    count += element.count
                    element.checked = false
                });
                state.commit('setBadge', count)
                state.commit('setCarList', list)

            });
        },
        //处理单选事件
        changeChecked(state) {
            let checked = true
            state.getters.getCartData.forEach(item => {
                if (!item.checked) {
                    checked = false
                }
            })
            state.commit('setAllchecked', checked)
        },
        //处理全选事件
        changeAllChecked(data) {
            data.state.carList.forEach(item => {
                    //改变购物车数据的选择状态
                    item.checked = !data.state.allChecked
                })
                //改变全选状态
            data.commit('setAllchecked', !data.state.allChecked)
        },
    },
});